<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ConsultController
 *
 * @author lzx
 */
require_once BaseUtil::getModulePath('base').'/controllers/BaseController.php';
require_once BaseUtil::getModulePath('consult').'/forms/EditForm.php';
require_once BaseUtil::getModulePath('consult').'/forms/AddForm.php';
require_once BaseUtil::getModulePath('consult').'/forms/IndexForm.php';
require_once BaseUtil::getModulePath('consult').'/services/ConsultService.php';
require_once BaseUtil::getModulePath('consult').'/constants/ConsultConstant.php';
require_once BaseUtil::getModulePath('cms').'/forms/FeaturePageForm.php';

class Consult_ConsultController extends BaseController{
    //put your code here
    public function init(){
        $this->view->script = array('lib/ui.dialog.js','inventory.js');
        parent::init();        
    }
    
    public function indexAction(){
        $consultService = new ConsultService();
        $queries = $consultService->getQueries($this->view,$_POST);
        $this->view->queries=$queries;
    }
    
    public function addAction(){
        $addForm = new AddForm();
        $this->view->form=$addForm;
    }
    
    public function subaddAction(){
        $addForm = new AddForm();
        $consultService = new ConsultService();
        if(!$addForm->isValid($_POST)){
            $this->view->form=$addForm;
            return $this->render('add');
        }
        $consultService->addConsult($addForm);
        $this->_redirect('/consult/consult/index');
        exit();
    }
    
    public function editAction(){
        $consultService = new ConsultService();
        $queryId = $this->_getParam("id");
        $editForm = new EditForm();
        $this->view->form=$editForm;
        $editForm->getElement("id")->setValue($queryId);//queryid
        //get query
        $query = $consultService->getQueryById($queryId);
        //get query reply
        $reply = $consultService->getReplyByQueryId($queryId);
        if(!empty($reply['content'])){
        	$editForm->getElement("content")->setValue($reply['content']);
        }
        
        
        if(!empty($reply)){//if is not null
            $user = BaseUtil::getLoginUser();
            $userid = $user['uid'];
//            $editForm->getElement("replyId")->setValue($reply['id']);            
            $editForm->getElement("replyId")->setValue($userid);//set the replyId is current admin id
            //$editForm->getElement("content")->setValue($reply['content']);
        }
        $this->view->query = $query;
        $this->view->reply = $reply;
        $this->view->id = $queryId;
    }
    
    public function subeditAction(){
        $queryId = $this->_getParam("id");
        $editForm = new EditForm();
        $consultService = new ConsultService();        
        if(!$editForm->isValid($_POST)){
            $this->view->form=$editForm;
            return $this->render('edit');
        }
        $consultService->editConsult($editForm,$queryId);
        $this->_redirect('/consult/consult/index');
        //exit();
    }
    
    public function deleteAction(){
        $queryId = $this->_getParam('id');
        $consultService = new ConsultService();
        $consultService->deleteQueries($queryId);
        $this->_redirector->gotoUrl('consult/consult/');
    }
    
    
}
?>
